package biznussoft.metercor.fragments;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import biznussoft.metercor.MainActivity;
import biznussoft.metercor.R;
import biznussoft.metercor.entity.AccountModel;
import biznussoft.metercor.entity.ActivityModel;
import biznussoft.metercor.entity.MapModel;
import biznussoft.metercor.entity.ServiceOrderModel;
import biznussoft.metercor.fragments.ServiceOrderRecordsFragment.MapTask;
import biznussoft.metercor.lookup.ManufacturerLookUp;
import biznussoft.metercor.lookup.ModelLookup;
import biznussoft.metercor.utils.Session;

import com.google.gson.Gson;
import com.salesforce.androidsdk.rest.RestClient;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;
import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.util.EventsObservable;
import com.salesforce.androidsdk.util.EventsObservable.EventType;

public class NewWaterMeterDataManagement extends Fragment implements OnClickListener, OnItemSelectedListener {
	private RestClient client;
	private EditText customerSuppliedData3,customerSuppliedData4,
	customerSuppliedData5,customerSuppliedData6,customerSuppliedData7,customerSuppliedData8,customerSuppliedData9;
	private Spinner installer1,installer2,installer3,installer4,installer5,installer6,installer7,installer8,installer9;
	private EditText verificationUpdated1,verificationUpdated2,verificationUpdated3,verificationUpdated4,verificationUpdated5,verificationUpdated6,verificationUpdated7,verificationUpdated8,verificationUpdated9;
	private EditText newEquipment3,newEquipment4,newEquipment5,newEquipment6,newEquipment7,newEquipment8,newEquipment9;
	private Button waterSave;

	public static EditText customerSuppliedData1,newEquipment1,newEquipment2,customerSuppliedData2;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.water_meter, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		client = MainActivity.mActivity.client;
		AccountModel.api_Version = getString(R.string.api_version);
		
		
		
		retriveLeads();
		

		customerSuppliedData1 = (EditText) getView().findViewById(R.id.customerSuppliedData1);
		customerSuppliedData2 = (EditText) getView().findViewById(R.id.customerSuppliedData2);
		customerSuppliedData3 = (EditText) getView().findViewById(R.id.customerSuppliedData3);
		customerSuppliedData4 = (EditText) getView().findViewById(R.id.customerSuppliedData4);
		customerSuppliedData5 = (EditText) getView().findViewById(R.id.customerSuppliedData5);
		customerSuppliedData6 = (EditText) getView().findViewById(R.id.customerSuppliedData6);
		customerSuppliedData7 = (EditText) getView().findViewById(R.id.customerSuppliedData7);
		customerSuppliedData8 = (EditText) getView().findViewById(R.id.customerSuppliedData8);
		customerSuppliedData9 = (EditText) getView().findViewById(R.id.customerSuppliedData9);


		verificationUpdated1 = (EditText) getView().findViewById(R.id.verificationUpdated1);
		verificationUpdated2 = (EditText) getView().findViewById(R.id.verificationUpdated2);
		verificationUpdated3 = (EditText) getView().findViewById(R.id.verificationUpdated3);
		verificationUpdated4 = (EditText) getView().findViewById(R.id.verificationUpdated4);
		verificationUpdated5 = (EditText) getView().findViewById(R.id.verificationUpdated5);
		verificationUpdated6 = (EditText) getView().findViewById(R.id.verificationUpdated6);
		verificationUpdated7 = (EditText) getView().findViewById(R.id.verificationUpdated7);
		verificationUpdated8 = (EditText) getView().findViewById(R.id.verificationUpdated8);
		verificationUpdated9 = (EditText) getView().findViewById(R.id.verificationUpdated9);


		newEquipment1 = (EditText) getView().findViewById(R.id.newEquipment1);
		newEquipment2 = (EditText) getView().findViewById(R.id.newEquipment2);
		newEquipment3 = (EditText) getView().findViewById(R.id.newEquipment3);
		newEquipment4 = (EditText) getView().findViewById(R.id.newEquipment4);
		newEquipment5 = (EditText) getView().findViewById(R.id.newEquipment5);
		newEquipment6 = (EditText) getView().findViewById(R.id.newEquipment6);
		newEquipment7 = (EditText) getView().findViewById(R.id.newEquipment7);
		newEquipment8 = (EditText) getView().findViewById(R.id.newEquipment8);
		newEquipment9 = (EditText) getView().findViewById(R.id.newEquipment9);


		installer1= (Spinner) getView().findViewById(R.id.installer1);
		installer2= (Spinner) getView().findViewById(R.id.installer2);
		installer3= (Spinner) getView().findViewById(R.id.installer3);
		installer4= (Spinner) getView().findViewById(R.id.installer4);
		installer5= (Spinner) getView().findViewById(R.id.installer5);
		installer6= (Spinner) getView().findViewById(R.id.installer6);
		installer7= (Spinner) getView().findViewById(R.id.installer7);
		installer8= (Spinner) getView().findViewById(R.id.installer8);
		installer9= (Spinner) getView().findViewById(R.id.installer9);

		installer1.setOnItemSelectedListener(this);
		installer2.setOnItemSelectedListener(this);
		installer3.setOnItemSelectedListener(this);
		installer4.setOnItemSelectedListener(this);
		installer5.setOnItemSelectedListener(this);
		installer6.setOnItemSelectedListener(this);
		installer7.setOnItemSelectedListener(this);
		installer8.setOnItemSelectedListener(this);
		installer9.setOnItemSelectedListener(this);


		waterSave = (Button) getView().findViewById(R.id.waterSave);

		waterSave.setOnClickListener(this);

		customerSuppliedData1.setOnClickListener(this);
		customerSuppliedData2.setOnClickListener(this);
		newEquipment1.setOnClickListener(this);
		newEquipment2.setOnClickListener(this);



	}
	
	
	
	public void retriveLeads()        
	{
		try{        
			
			String serviceOrder = "select id,name, Meter_Manufacturer__c,Meter_Model__c,Meter_Size__c,Meter_Tag__c,Meter_Head_Serial__c,Meter_Body_Serial__c, ERT_Serial__c, Meter_Head_Reading__c, ERT_Reading__c FROM Account where id ="+"'"+Session.sAccountId+"'";

			RestRequest restRequest = RestRequest.getRequestForQuery(getString(R.string.api_version), serviceOrder);
			
			client.sendAsync(restRequest, new AsyncRequestCallback() {  
				@Override
				public void onSuccess(RestRequest request, RestResponse result) {
					try {
						Log.i("NCS", "Response is  :"+result);
						JSONObject jObject = new JSONObject(result.toString());
						JSONArray jArray = jObject.getJSONArray("records");
						customerSuppliedData1.setText(jArray.getJSONObject(0).getString("Meter_Manufacturer__c")); 
						customerSuppliedData2.setText(jArray.getJSONObject(0).getString("Meter_Model__c")); 
						customerSuppliedData3.setText(jArray.getJSONObject(0).getString("Meter_Size__c")); 
						customerSuppliedData4.setText(jArray.getJSONObject(0).getString("Meter_Tag__c")); 
						customerSuppliedData5.setText(jArray.getJSONObject(0).getString("Meter_Head_Serial__c")); 
						customerSuppliedData6.setText(jArray.getJSONObject(0).getString("Meter_Body_Serial__c")); 
						customerSuppliedData7.setText(jArray.getJSONObject(0).getString("ERT_Serial__c")); 
						customerSuppliedData8.setText(jArray.getJSONObject(0).getString("Meter_Head_Reading__c")); 
						customerSuppliedData9.setText(jArray.getJSONObject(0).getString("ERT_Reading__c")); 
						
					} catch (Exception e) {
						Log.i("NCS","Exception is  :"+e.getMessage());
						onError(e);
					}
				}

				@Override
				public void onError(Exception exception) {
					EventsObservable.get().notifyEvent(EventType.RenditionComplete);
				}
			});



		} catch (Exception e) {
			Log.i("NCS","Exception  2 is  :"+e.getMessage());
		}
	}
	

	@Override
	public void onClick(View v) {

		switch (v.getId()) {

		case R.id.customerSuppliedData2:
			ModelLookup modelLookup = new ModelLookup();
			modelLookup.getItemLookUp(getActivity(), "");
			break;
		case R.id.customerSuppliedData1:
			ManufacturerLookUp mn = new ManufacturerLookUp();
			mn.getItemLookUp(getActivity(), "customer");
			break;
		case R.id.newEquipment1:
			ManufacturerLookUp mn1 = new ManufacturerLookUp();
			mn1.getItemLookUp(getActivity(), "equ1");

			break;
		case R.id.newEquipment2:
			ManufacturerLookUp mn2 = new ManufacturerLookUp();
			mn2.getItemLookUp(getActivity(), "equ2");
			break;

		case R.id.waterSave:

			insertWaterMeter();
			break;

		default:
			break;
		}
	}

	public void insertWaterMeter(){

		String objectType = "Meter_Data_Management__c";
		HashMap<String, Object> fields = new HashMap<String, Object>();
		fields.put("Meter_Manufacturer__c",customerSuppliedData1.getText().toString());
		fields.put("Meter_Model__c", customerSuppliedData2.getText().toString());
		fields.put("Service_Order__c", Session.sSerOrderId);                 
		fields.put("ERT_Reading_Verification__c", installer8.getSelectedItem().toString());
		fields.put("ERT_Serial_Verification__c", installer6.getSelectedItem().toString());
		fields.put("MBS_Verification__c", installer5.getSelectedItem().toString());
		fields.put("Meter_Model_Verification__c", installer2.getSelectedItem().toString());
		fields.put("Meter_Size_Verification__c", installer3.getSelectedItem().toString());
		fields.put("Meter_Tag_Verification__c", installer4.getSelectedItem().toString());
		fields.put("MHR_Verification__c", installer7.getSelectedItem().toString());
		fields.put("MMftur_Verification__c", installer1.getSelectedItem().toString());
		fields.put("Meter_Size__c",customerSuppliedData3.getText().toString());
		fields.put("Meter_Tag__c",customerSuppliedData4.getText().toString());
		fields.put("Meter_Head_Serial__c",customerSuppliedData5.getText().toString());
		fields.put("Meter_Body_Serial__c",customerSuppliedData6.getText().toString());
		fields.put("ERT_Serial__c",customerSuppliedData7.getText().toString());
		fields.put("Meter_Head_Reading__c",customerSuppliedData8.getText().toString());
		fields.put("ERT_Reading__c",customerSuppliedData9.getText().toString());

		try {
			RestRequest restRequest = RestRequest.getRequestForCreate(
					getString(R.string.api_version), objectType, fields);

			client.sendAsync(restRequest, new AsyncRequestCallback() {
				@Override
				public void onSuccess(RestRequest request, RestResponse result) {
					try {
						Log.i("NCS", "Response is  :" + result);
						//finishFragment();
					} catch (Exception e) {
						Log.i("TPT", "Exception is   :" + e.getMessage());
					}
				}

				@Override
				public void onError(Exception exception) {
					EventsObservable.get().notifyEvent(
							EventType.RenditionComplete);
				}
			});
		} catch (Exception e) {
			Log.i("NCS", "Exception is   :" + e.getMessage());

		}

	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View v, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
		switch (arg0.getId()) {
		case R.id.installer1:
			Log.i("NCS", "inselected");

			if(installer1.getSelectedItem().toString().equals("INCORRECT")){
				Log.i("NCS", "VISIBLE");
				verificationUpdated1.setVisibility(View.VISIBLE);
			}else{
				Log.i("NCS", "INVISIBLE");
				verificationUpdated1.setVisibility(View.INVISIBLE);
			}

			break;
		case R.id.installer2:
			if(installer1.getSelectedItem().toString().equals("INCORRECT")){
				verificationUpdated2.setVisibility(View.VISIBLE);
			}else{
				verificationUpdated2.setVisibility(View.INVISIBLE);
			}
			break;
		case R.id.installer3:
			if(installer1.getSelectedItem().toString().equals("INCORRECT")){
				verificationUpdated3.setVisibility(View.VISIBLE);
			}else{
				verificationUpdated3.setVisibility(View.INVISIBLE);
			}
			break;
		case R.id.installer4:
			if(installer1.getSelectedItem().toString().equals("INCORRECT")){
				verificationUpdated4.setVisibility(View.VISIBLE);
			}else{
				verificationUpdated4.setVisibility(View.INVISIBLE);
			}
			break;
		case R.id.installer5:
			if(installer1.getSelectedItem().toString().equals("INCORRECT")){
				verificationUpdated5.setVisibility(View.VISIBLE);
			}else{
				verificationUpdated5.setVisibility(View.INVISIBLE);
			}
			break;
		case R.id.installer6:
			if(installer1.getSelectedItem().toString().equals("INCORRECT")){
				verificationUpdated6.setVisibility(View.VISIBLE);
			}else{
				verificationUpdated6.setVisibility(View.INVISIBLE);
			}
			break;
		case R.id.installer7:
			if(installer1.getSelectedItem().toString().equals("INCORRECT")){
				verificationUpdated7.setVisibility(View.VISIBLE);
			}else{
				verificationUpdated7.setVisibility(View.INVISIBLE);
			}
			break;
		case R.id.installer8:
			if(installer1.getSelectedItem().toString().equals("INCORRECT")){
				verificationUpdated8.setVisibility(View.VISIBLE);
			}else{
				verificationUpdated8.setVisibility(View.INVISIBLE);
			}
			break;
		case R.id.installer9:
			if(installer1.getSelectedItem().toString().equals("INCORRECT")){
				verificationUpdated9.setVisibility(View.VISIBLE);
			}else{
				verificationUpdated9.setVisibility(View.INVISIBLE);
			}
			break;


		default:
			break;
		}

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}


}
